﻿Imports System.Management

NotInheritable Class SystemInfo
    Private Sub New()
    End Sub

#Region "-> Private Variables"

    Public Shared UseProcessorID As Boolean
    Public Shared UseBaseBoardProduct As Boolean
    Public Shared UseBaseBoardManufacturer As Boolean
    Public Shared UseDiskDriveSignature As Boolean
    Public Shared UseVideoControllerCaption As Boolean
    Public Shared UsePhysicalMediaSerialNumber As Boolean
    Public Shared UseBiosVersion As Boolean
    Public Shared UseBiosManufacturer As Boolean
    Public Shared UseWindowsSerialNumber As Boolean

#End Region

    Public Shared Function GetSystemInfo(ByVal SoftwareName As String) As String
        If UseProcessorID = True Then
            SoftwareName += RunQuery("Processor", "ProcessorId")
        End If

        If UseBaseBoardProduct = True Then
            SoftwareName += RunQuery("BaseBoard", "Product")
        End If

        If UseBaseBoardManufacturer = True Then
            SoftwareName += RunQuery("BaseBoard", "Manufacturer")
        End If

        If UseDiskDriveSignature = True Then
            SoftwareName += RunQuery("DiskDrive", "Signature")
        End If

        If UseVideoControllerCaption = True Then
            SoftwareName += RunQuery("VideoController", "Caption")
        End If

        If UsePhysicalMediaSerialNumber = True Then
            SoftwareName += RunQuery("PhysicalMedia", "SerialNumber")
        End If

        If UseBiosVersion = True Then
            SoftwareName += RunQuery("BIOS", "Version")
        End If

        If UseWindowsSerialNumber = True Then
            SoftwareName += RunQuery("OperatingSystem", "SerialNumber")
        End If

        SoftwareName = RemoveUseLess(SoftwareName)

        If SoftwareName.Length < 25 Then
            Return GetSystemInfo(SoftwareName)
        End If

        Return SoftwareName.Substring(0, 25).ToUpper()
    End Function

    Private Shared Function RemoveUseLess(ByVal st As String) As String
        Dim ch As Char
        For i As Integer = st.Length - 1 To 0 Step - 1
            ch = Char.ToUpper(st(i))

            If (ch < "A"c OrElse ch > "Z"c) AndAlso (ch < "0"c OrElse ch > "9"c) Then
                st = st.Remove(i, 1)
            End If
        Next
        Return st
    End Function

    Friend Shared CachedQueryResult As Dictionary(Of String, String)

    Private Shared Function RunQuery(ByVal TableName As String, ByVal MethodName As String) As String
        Dim MOS As New ManagementObjectSearcher("Select * from Win32_" & TableName)
        Dim strResult As String = Nothing
        For Each MO As ManagementObject In MOS.[Get]()
            Try
                If CachedQueryResult Is Nothing Then
                    CachedQueryResult = New Dictionary(Of String, String)
                End If

                If CachedQueryResult.ContainsKey(MethodName) Then
                    strResult = CachedQueryResult(MethodName)
                Else
                    strResult = MO(MethodName).ToString()
                    CachedQueryResult.Add(MethodName, strResult)
                End If
                Return strResult
            Catch e As Exception
                Throw
            End Try
        Next
        Return ""
    End Function
End Class

